#!/bin/bash

echo "=========================================================================="
echo "                   GitHub 和 GitCode 多平台推送配置脚本                    "
echo "=========================================================================="

# 配置GitHub全局访问令牌
echo "[1/3] 配置GitHub全局认证..."
git config --global url.https://wanghj-dz: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@github.com/.insteadOf https://github.com/
echo "GitHub全局认证配置完成！"

# 配置GitCode全局访问令牌（检查是否已配置）
echo "[2/3] 配置GitCode全局认证..."
GITCODE_CONFIGURED=$(git config --global --get-all url.https://wanghj_dz:mKa6mqkfzvVkQQo9FbUi1hio@gitcode.com/.insteadOf)

if [ -z "$GITCODE_CONFIGURED" ]; then
    git config --global url.https://wanghj_dz:mKa6mqkfzvVkQQo9FbUi1hio@gitcode.com/.insteadOf https://gitcode.com/
    echo "GitCode全局认证配置完成！"
else
    echo "GitCode全局认证已存在，跳过配置。"
fi

echo "[3/3] 配置多远程仓库推送..."

echo "请输入GitHub仓库名（不需要完整URL，只需要仓库名称）:"
read repo_name

# 构建完整的仓库URL
github_url="https://github.com/wanghj-dz/${repo_name}.git"
gitcode_url="https://gitcode.com/wanghj_dz/${repo_name}.git"

echo "正在配置远程仓库..."

# 检查是否已有origin远程仓库
if git remote | grep -q "origin"; then
    echo "检测到已存在origin远程仓库，正在更新配置..."
    git remote set-url origin $github_url
    git remote set-url --add --push origin $github_url
    git remote set-url --add --push origin $gitcode_url
else
    echo "添加新的origin远程仓库..."
    git remote add origin $github_url
    git remote set-url --add --push origin $github_url
    git remote set-url --add --push origin $gitcode_url
fi

echo "已配置的远程仓库:"
git remote -v

echo "=========================================================================="
echo "配置完成！现在您可以使用以下命令同时推送到GitHub和GitCode："
echo "  git push origin 分支名 （例如：git push origin main）"
echo "=========================================================================="
echo "提示：如果需要查看当前分支名，请执行 git branch"
echo "=========================================================================="